/*
* @Author: karry
* @Date:   2019-08-01 14:42:55
* @Last Modified by:   karry
* @Last Modified time: 2019-08-01 14:42:55
*/
import React from 'react';
import {Link} from 'react-router-dom';
import MUtil 	from 'util/db.jsx';
import User 	from 'service/user-service.jsx'

const _db 	= new MUtil();
const _user = new User();

class NavTop extends React.Component{
	constructor(props){
		super(props);
		this.state = {
			username : _db.getStorage('userInfo').username || ''
		}
	}
	// 退出登录
	onLogout(){
		_user.logout().then(res => {
			_db.removeStorage('userInfo');
			window.location.href = '/login';
		}, errMsg => { 
			_db.errorTips(errMsg);
		});
	}
	render(){
		return (
			<div className="navbar navbar-default top-navbar">
	            <div className="navbar-header">
	                <button type="button" className="navbar-toggle" data-toggle="collapse" data-target=".sidebar-collapse">
	                    <span className="sr-only">Toggle navigation</span>
	                    <span className="icon-bar"></span>
	                    <span className="icon-bar"></span>
	                    <span className="icon-bar"></span>
	                </button>
	                <Link className="navbar-brand" to="/"><b>BAYMAX</b>MALL</Link>
				<div id="sideNav" href=""><i className="fa fa-caret-right"></i></div>
	            </div>
	            <ul className="nav navbar-top-links navbar-right">
	                <li className="dropdown">
	                    <a className="dropdown-toggle"  href="javascript:;">
	                        <i className="fa fa-user fa-fw"></i>
	                        {
	                        	this.state.username ? <span>欢迎，{this.state.username}</span> : <span>欢迎您来到DBMALL</span>
	                        }
	                        <i className="fa fa-caret-down"></i>
	                    </a>
	                    <ul className="dropdown-menu dropdown-user">
	                        <li>
	                        	<a onClick={() => {this.onLogout()}}>
	                        		<i className="fa fa-sign-out fa-fw"></i>
	                        		<span>退出登录</span>
	                        	</a>
	                        </li>
	                    </ul>
	                </li>
	            </ul>
	        </div>
		);
	}
}

export default NavTop;